package com.kdgc.power.gen;


import org.beetl.core.Template;
import org.beetl.sql.gen.Attribute;
import org.beetl.sql.gen.BaseProject;
import org.beetl.sql.gen.Entity;
import org.beetl.sql.gen.SourceConfig;
import org.beetl.sql.gen.simple.BaseTemplateSourceBuilder;

import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 表对应实体的Java代码
 */
public class EntitySourceBuilder extends BaseTemplateSourceBuilder {
    /**
     * 指定模板的路径
     */
    public static String entityPath = "entity.btl";

    public EntitySourceBuilder() {
        super("entity");
    }


    @Override
    public void generate(BaseProject project, SourceConfig config, Entity entity) {
        List<String> commonNames = Arrays.asList("isUse", "modifyTime", "createTime");
        Template template = groupTemplate.getTemplate(entityPath);
        ArrayList<Attribute> list = entity.getList();
        list.removeIf(attribute -> commonNames.contains(attribute.getName()));
        template.binding("attrs", entity.getList());
        template.binding("className", entity.getName());
        template.binding("package", project.getBasePackage(this.name));
        template.binding("imports", entity.getImportPackage());
        template.binding("comment", entity.getComment());
        Writer writer = project.getWriterByName(this.name, entity.getName() + ".java");
        template.renderTo(writer);


    }
}

